package com.example.onlineeducation.adapter

import com.example.onlineeducation.R
import com.example.onlineeducation.databinding.StudyPractiseItemBinding
import com.yc.jetpacklib.recycleView.YcRecyclerViewAdapter
import com.yc.jetpacklib.utils.YcResources
import java.util.*

/**
 * SimpleDes:
 * Creator: Sindi
 * Date: 2022-04-08
 * UseDes:
 */
class SelectorButtonAdapter(private var mIsSingle: Boolean) : YcRecyclerViewAdapter<String, StudyPractiseItemBinding>(StudyPractiseItemBinding::inflate) {
    private var mSelectPosition = -1
    private var mPreSelectTime = 0L
    private val mMultipleSelectPositionList: MutableList<Int> = ArrayList() //记录多选的选项位置


    fun setSelect(position: Int, isRefresh: Boolean) {
        val currentTime = System.currentTimeMillis()
        if (currentTime - mPreSelectTime > 100) { //防止按键抖动
            mPreSelectTime = currentTime
            if (position < mData.size) {
                if (mIsSingle) {
                    //需要刷新上一条和当前选中的那个
                    val prePosition = mSelectPosition
                    mSelectPosition = position
                    if (isRefresh && prePosition < mData.size) notifyItemChanged(prePosition)
                    if (isRefresh) notifyItemChanged(mSelectPosition)
                } else {
                    if (mMultipleSelectPositionList.contains(position)) {
                        for (i in mMultipleSelectPositionList.indices) {
                            if (mMultipleSelectPositionList[i] == position) {
                                mMultipleSelectPositionList.removeAt(i)
                                notifyItemChanged(position)
                                break
                            }
                        }
                    } else {
                        mMultipleSelectPositionList.add(position)
                        notifyItemChanged(position)
                    }
                }
            }
        }
    }

    override var mOnUpdate2: (StudyPractiseItemBinding.(position: Int, data: String) -> Unit)? = { position: Int, data: String ->
        tvContent.text = data
        if (mIsSingle) {
            if (mSelectPosition == position) {
                tvContent.setTextColor(YcResources.getColorRes(R.color.selector_box_select))
                clSelect.setBackgroundResource(R.drawable.select_box_bg_pass)
                ivSelect.setImageResource(R.drawable.ic_result_right2)
            } else {
                tvContent.setTextColor(YcResources.getColorRes(R.color.selector_box_default))
                clSelect.setBackgroundResource(R.drawable.select_box_bg_unpass)
                ivSelect.setImageResource(R.drawable.select_box_single_unpass)
            }
        } else {
            if (mMultipleSelectPositionList.contains(position)) {
                tvContent.setTextColor(YcResources.getColorRes(R.color.selector_box_select))
                clSelect.setBackgroundResource(R.drawable.select_box_bg_pass)
                ivSelect.setBackgroundResource(R.drawable.select_box_multiple_pass)
            } else {
                tvContent.setTextColor(YcResources.getColorRes(R.color.selector_box_default))
                clSelect.setBackgroundResource(R.drawable.select_box_bg_unpass)
                ivSelect.setBackgroundResource(R.drawable.select_box_multiple_unpass)
            }
        }
    }
}